home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlre.1 < prev    next >
Text File  |  1995-07-25  |  20KB  |  463 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlre - Perl regular expressions
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.           For a description of how to use regular expressions in
  13.           matching operations, see m// and s/// in the _p_e_r_l_o_p manpage.
  14.           The matching operations can have various modifiers, some of
  15.           which relate to the interpretation of the regular expression
  16.           inside.  These are:
  17.  
  18.               i   Do case-insensitive pattern matching.
  19.               m   Treat string as multiple lines.
  20.               s   Treat string as single line.
  21.               x   Use extended regular expressions.
  22.  
  23.           These are usually written as "the /x modifier", even though
  24.           the delimiter in question might not actually be a slash.  In
  25.           fact, any of these modifiers may also be embedded within the
  26.           regular expression itself using the new (?...) construct.
  27.           See below.
  28.  
  29.           The /x modifier itself needs a little more explanation.  It
  30.           tells the regular expression parser to ignore whitespace
  31.           that is not backslashed or within a character class.  You
  32.           can use this to break up your regular expression into
  33.           (slightly) more readable parts.  Together with the
  34.           capability of embedding comments described later, this goes
  35.           a long way towards making Perl 5 a readable language.  See
  36.           the C comment deletion code in the _p_e_r_l_o_p manpage.
  37.  
  38.           RRRReeeegggguuuullllaaaarrrr EEEExxxxpppprrrreeeessssssssiiiioooonnnnssss
  39.  
  40.           The patterns used in pattern matching are regular
  41.           expressions such as those supplied in the Version 8 regexp
  42.           routines.  (In fact, the routines are derived (distantly)
  43.           from Henry Spencer's freely redistributable reimplementation
  44.           of the V8 routines.) See the section on _V_e_r_s_i_o_n _8 _R_e_g_u_l_a_r
  45.           _E_x_p_r_e_s_s_i_o_n_s for details.
  46.  
  47.           In particular the following metacharacters have their
  48.           standard _e_g_r_e_p-ish meanings:
  49.  
  50.               \   Quote the next metacharacter
  51.               ^   Match the beginning of the line
  52.               .   Match any character (except newline)
  53.               $   Match the end of the line
  54.               |   Alternation
  55.               ()  Grouping
  56.               []  Character class
  57.  
  58.           By default, the "^" character is guaranteed to match only at
  59.           the beginning of the string, the "$" character only at the
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  71.  
  72.  
  73.  
  74.           end (or before the newline at the end) and Perl does certain
  75.           optimizations with the assumption that the string contains
  76.           only one line.  Embedded newlines will not be matched by "^"
  77.           or "$".  You may, however, wish to treat a string as a
  78.           multi-line buffer, such that the "^" will match after any
  79.           newline within the string, and "$" will match before any
  80.           newline.  At the cost of a little more overhead, you can do
  81.           this by using the /m modifier on the pattern match operator.
  82.           (Older programs did this by setting $*, but this practice is
  83.           deprecated in Perl 5.)
  84.  
  85.           To facilitate multi-line substitutions, the "." character
  86.           never matches a newline unless you use the /s modifier,
  87.           which tells Perl to pretend the string is a single line--
  88.           even if it isn't.  The /s modifier also overrides the
  89.           setting of $*, in case you have some (badly behaved) older
  90.           code that sets it in another module.
  91.  
  92.           The following standard quantifiers are recognized:
  93.  
  94.               *      Match 0 or more times
  95.               +      Match 1 or more times
  96.               ?      Match 1 or 0 times
  97.               {n}    Match exactly n times
  98.               {n,}   Match at least n times
  99.               {n,m}  Match at least n but not more than m times
  100.  
  101.           (If a curly bracket occurs in any other context, it is
  102.           treated as a regular character.)  The "*" modifier is
  103.           equivalent to {0,}, the "+" modifier to {1,}, and the "?"
  104.           modifier to {0,1}.  There is no limit to the size of n or m,
  105.           but large numbers will chew up more memory.
  106.  
  107.           By default, a quantified subpattern is "greedy", that is, it
  108.           will match as many times as possible without causing the
  109.           rest pattern not to match.  The standard quantifiers are all
  110.           "greedy", in that they match as many occurrences as possible
  111.           (given a particular starting location) without causing the
  112.           pattern to fail.  If you want it to match the minimum number
  113.           of times possible, follow the quantifier with a "?" after
  114.           any of them.  Note that the meanings don't change, just the
  115.           "gravity":
  116.  
  117.               *?     Match 0 or more times
  118.               +?     Match 1 or more times
  119.               ??     Match 0 or 1 time
  120.               {n}?   Match exactly n times
  121.               {n,}?  Match at least n times
  122.               {n,m}? Match at least n but not more than m times
  123.  
  124.           Since patterns are processed as double quoted strings, the
  125.           following also work:
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  137.  
  138.  
  139.  
  140.               \t          tab
  141.               \n          newline
  142.               \r          return
  143.               \f          form feed
  144.               \v          vertical tab, whatever that is
  145.               \a          alarm (bell)
  146.               \e          escape
  147.               \033        octal char
  148.               \x1b        hex char
  149.               \c[         control char
  150.               \l          lowercase next char
  151.               \u          uppercase next char
  152.               \L          lowercase till \E
  153.               \U          uppercase till \E
  154.               \E          end case modification
  155.               \Q          quote regexp metacharacters till \E
  156.  
  157.           In addition, Perl defines the following:
  158.  
  159.               \w  Match a "word" character (alphanumeric plus "_")
  160.               \W  Match a non-word character
  161.               \s  Match a whitespace character
  162.               \S  Match a non-whitespace character
  163.               \d  Match a digit character
  164.               \D  Match a non-digit character
  165.  
  166.           Note that \w matches a single alphanumeric character, not a
  167.           whole word.  To match a word you'd need to say \w+.  You may
  168.           use \w, \W, \s, \S, \d and \D within character classes
  169.           (though not as either end of a range).
  170.  
  171.           Perl defines the following zero-width assertions:
  172.  
  173.               \b  Match a word boundary
  174.               \B  Match a non-(word boundary)
  175.               \A  Match only at beginning of string
  176.               \Z  Match only at end of string
  177.               \G  Match only where previous m//g left off
  178.  
  179.           A word boundary (\b) is defined as a spot between two
  180.           characters that has a \w on one side of it and and a \W on
  181.           the other side of it (in either order), counting the
  182.           imaginary characters off the beginning and end of the string
  183.           as matching a \W.  (Within character classes \b represents
  184.           backspace rather than a word boundary.)  The \A and \Z are
  185.           just like "^" and "$" except that they won't match multiple
  186.           times when the /m modifier is used, while "^" and "$" will
  187.           match at every internal line boundary.
  188.  
  189.           When the bracketing construct ( ... ) is used, \<digit>
  190.           matches the digit'th substring.  (Outside of the pattern,
  191.           always use "$" instead of "\" in front of the digit.  The
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  203.  
  204.  
  205.  
  206.           scope of $<digit> (and $`, $&, and $') extends to the end of
  207.           the enclosing BLOCK or eval string, or to the next pattern
  208.           match with subexpressions. If you want to use parentheses to
  209.           delimit subpattern (e.g. a set of alternatives) without
  210.           saving it as a subpattern, follow the ( with a ?.  The
  211.           \<digit> notation sometimes works outside the current
  212.           pattern, but should not be relied upon.)  You may have as
  213.           many parentheses as you wish.  If you have more than 9
  214.           substrings, the variables $10, $11, ... refer to the
  215.           corresponding substring.  Within the pattern, \10, \11, etc.
  216.           refer back to substrings if there have been at least that
  217.           many left parens before the backreference.  Otherwise (for
  218.           backward compatibilty) \10 is the same as \010, a backspace,
  219.           and \11 the same as \011, a tab.  And so on.  (\1 through \9
  220.           are always backreferences.)
  221.  
  222.           $+ returns whatever the last bracket match matched.  $&
  223.           returns the entire matched string.  ($0 used to return the
  224.           same thing, but not any more.)  $` returns everything before
  225.           the matched string.  $' returns everything after the matched
  226.           string.  Examples:
  227.  
  228.               s/^([^ ]*) *([^ ]*)/$2 $1/;     # swap first two words
  229.  
  230.               if (/Time: (..):(..):(..)/) {
  231.                   $hours = $1;
  232.                   $minutes = $2;
  233.                   $seconds = $3;
  234.               }
  235.  
  236.           You will note that all backslashed metacharacters in Perl
  237.           are alphanumeric, such as \b, \w, \n.  Unlike some other
  238.           regular expression languages, there are no backslashed
  239.           symbols that aren't alphanumeric.  So anything that looks
  240.           like \\, \(, \), \<, \>, \{, or \} is always interpreted as
  241.           a literal character, not a metacharacter.  This makes it
  242.           simple to quote a string that you want to use for a pattern
  243.           but that you are afraid might contain metacharacters.
  244.           Simply quote all the non-alphanumeric characters:
  245.  
  246.               $pattern =~ s/(\W)/\\$1/g;
  247.  
  248.           You can also use the built-in _q_u_o_t_e_m_e_t_a() function to do
  249.           this.  An even easier way to quote metacharacters right in
  250.           the match operator is to say
  251.  
  252.               /$unquoted\Q$quoted\E$unquoted/
  253.  
  254.           Perl 5 defines a consistent extension syntax for regular
  255.           expressions.  The syntax is a pair of parens with a question
  256.           mark as the first thing within the parens (this was a syntax
  257.           error in Perl 4).  The character after the question mark
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  269.  
  270.  
  271.  
  272.           gives the function of the extension.  Several extensions are
  273.           already supported:
  274.  
  275.           (?#text)  A comment.  The text is ignored.
  276.  
  277.           (?:regexp)
  278.                     This groups things like "()" but doesn't make
  279.                     backrefences like "()" does.  So
  280.  
  281.                         split(/\b(?:a|b|c)\b/)
  282.  
  283.                     is like
  284.  
  285.                         split(/\b(a|b|c)\b/)
  286.  
  287.                     but doesn't spit out extra fields.
  288.  
  289.           (?=regexp)
  290.                     A zero-width positive lookahead assertion.  For
  291.                     example, /\w+(?=\t)/ matches a word followed by a
  292.                     tab, without including the tab in $&.
  293.  
  294.           (?!regexp)
  295.                     A zero-width negative lookahead assertion.  For
  296.                     example /foo(?!bar)/ matches any occurrence of
  297.                     "foo" that isn't followed by "bar".  Note however
  298.                     that lookahead and lookbehind are NOT the same
  299.                     thing.  You cannot use this for lookbehind:
  300.                     /(?!foo)bar/ will not find an occurrence of "bar"
  301.                     that is preceded by something which is not "foo".
  302.                     That's because the (?!foo) is just saying that the
  303.                     next thing cannot be "foo"--and it's not, it's a
  304.                     "bar", so "foobar" will match.  You would have to
  305.                     do something like /(?foo)...bar/ for that.   We
  306.                     say "like" because there's the case of your "bar"
  307.                     not having three characters before it.  You could
  308.                     cover that this way: /(?:(?!foo)...|^..?)bar/.
  309.                     Sometimes it's still easier just to say:
  310.  
  311.                         if (/foo/ && $` =~ /bar$/)
  312.  
  313.  
  314.           (?imsx)   One or more embedded pattern-match modifiers.
  315.                     This is particularly useful for patterns that are
  316.                     specified in a table somewhere, some of which want
  317.                     to be case sensitive, and some of which don't.
  318.                     The case insensitive ones merely need to include
  319.                     (?i) at the front of the pattern.  For example:
  320.  
  321.                         $pattern = "foobar";
  322.                         if ( /$pattern/i )
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  335.  
  336.  
  337.  
  338.                         # more flexible:
  339.  
  340.                         $pattern = "(?i)foobar";
  341.                         if ( /$pattern/ )
  342.  
  343.  
  344.           The specific choice of question mark for this and the new
  345.           minimal matching construct was because 1) question mark is
  346.           pretty rare in older regular expressions, and 2) whenever
  347.           you see one, you should stop and "question" exactly what is
  348.           going on.  That's psychology...
  349.  
  350.           VVVVeeeerrrrssssiiiioooonnnn 8888 RRRReeeegggguuuullllaaaarrrr EEEExxxxpppprrrreeeessssssssiiiioooonnnnssss
  351.  
  352.           In case you're not familiar with the "regular" Version 8
  353.           regexp routines, here are the pattern-matching rules not
  354.           described above.
  355.  
  356.           Any single character matches itself, unless it is a
  357.           _m_e_t_a_c_h_a_r_a_c_t_e_r with a special meaning described here or
  358.           above.  You can cause characters which normally function as
  359.           metacharacters to be interpreted literally by prefixing them
  360.           with a "\" (e.g. "\." matches a ".", not any character; "\\"
  361.           matches a "\").  A series of characters matches that series
  362.           of characters in the target string, so the pattern blurfl
  363.           would match "blurfl" in the target string.
  364.  
  365.           You can specify a character class, by enclosing a list of
  366.           characters in [], which will match any one of the characters
  367.           in the list.  If the first character after the "[" is "^",
  368.           the class matches any character not in the list.  Within a
  369.           list, the "-" character is used to specify a range, so that
  370.           a-z represents all the characters between "a" and "z",
  371.           inclusive.
  372.  
  373.           Characters may be specified using a metacharacter syntax
  374.           much like that used in C: "\n" matches a newline, "\t" a
  375.           tab, "\r" a carriage return, "\f" a form feed, etc.  More
  376.           generally, \_n_n_n, where _n_n_n is a string of octal digits,
  377.           matches the character whose ASCII value is _n_n_n.  Similarly,
  378.           \x_n_n, where _n_n are hexidecimal digits, matches the character
  379.           whose ASCII value is _n_n. The expression \c_x matches the
  380.           ASCII character control-_x.  Finally, the "." metacharacter
  381.           matches any character except "\n" (unless you use /s).
  382.  
  383.           You can specify a series of alternatives for a pattern using
  384.           "|" to separate them, so that fee|fie|foe will match any of
  385.           "fee", "fie", or "foe" in the target string (as would
  386.           f(e|i|o)e).  Note that the first alternative includes
  387.           everything from the last pattern delimiter ("(", "[", or the
  388.           beginning of the pattern) up to the first "|", and the last
  389.           alternative contains everything from the last "|" to the
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLRRRREEEE((((1111))))   UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))   PPPPEEEERRRRLLLLRRRREEEE((((1111))))
  401.  
  402.  
  403.  
  404.           next pattern delimiter.  For this reason, it's common
  405.           practice to include alternatives in parentheses, to minimize
  406.           confusion about where they start and end.  Note also that
  407.           the pattern (fee|fie|foe) differs from the pattern
  408.           [fee|fie|foe] in that the former matches "fee", "fie", or
  409.           "foe" in the target string, while the latter matches
  410.           anything matched by the classes [fee], [fie], or [foe] (i.e.
  411.           the class [feio]).
  412.  
  413.           Within a pattern, you may designate subpatterns for later
  414.           reference by enclosing them in parentheses, and you may
  415.           refer back to the _nth subpattern later in the pattern using
  416.           the metacharacter \_n. Subpatterns are numbered based on the
  417.           left to right order of their opening parenthesis.  Note that
  418.           a backreference matches whatever actually matched the
  419.           subpattern in the string being examined, not the rules for
  420.           that subpattern.  Therefore, ([0|0x])\d*\s\1\d* will match
  421.           "0x1234 0x4321",but not "0x1234 01234", since subpattern 1
  422.           actually matched "0x", even though the rule [0|0x] could
  423.           potentially match the leading 0 in the second number.
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.      Page 7                                          (printed 6/30/95)
  460.  
  461.  
  462.  
  463.